<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="100px">
            <el-form-item label="状态：">
              <el-radio-group
                v-model="tableForm.status_tag"
                type="button"
                @change="getList"
              >
                <el-radio-button label>全部</el-radio-button>
                <el-radio-button label="1">编辑中</el-radio-button>
                <el-radio-button label="2">审核中</el-radio-button>
                <el-radio-button label="3">审核失败</el-radio-button>
                <el-radio-button label="4">审核成功</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="关键字：" class="width100">
              <el-input
                v-model="tableForm.keyword"
                @keyup.enter.native="getList"
                placeholder="请输入商品ID"
                class="selWidth"
                size="small"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  class="el-button-solt"
                  @click="getList"
                />
              </el-input>
            </el-form-item>
          </el-form>
          <el-button size="small" type="primary" @click="dialogVisible = true">添加商品</el-button>
          <el-button size="small" type="success" @click="sync"
            >同步商品</el-button
          >
          
        </div>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="small"
        highlight-current-row
      >
        <el-table-column label="序号" min-width="60">
          <template scope="scope">
            <span>{{
              scope.$index + (tableForm.page - 1) * tableForm.limit + 1
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="out_product_id" label="商品ID" min-width="60" />
        <el-table-column prop="product.store_name" label="商品名称" min-width="200" />
        <el-table-column label="商品图" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                :src="scope.row.product.image"
                :preview-src-list="[scope.row.product.image]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="微信商品类目" min-width="220">
          <template slot-scope="scope">
            <span>{{ scope.row.cat_name }}</span> | 
            <el-button
              type="text"
              size="small"
              @click="tochangeCate(scope.row.out_product_id)"
              >修改</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="原价" min-width="90">
          <template slot-scope="scope">
            <span>{{ scope.row.product ? scope.row.product.ot_price : "" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="售价" min-width="90" >
            <template slot-scope="scope">
            <span>{{ scope.row.product ? scope.row.product.price : "" }}</span>
          </template>
        </el-table-column>
        <el-table-column label="库存" min-width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.product.stock }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="提交时间" min-width="160" />
        <el-table-column label="商品状态" min-width="100" prop="status_name" />
        <el-table-column
          key="3"
          label="审核状态"
          min-width="100"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.edit_status_name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="audit_time" label="审核时间" min-width="160" />
        <el-table-column label="操作" min-width="150" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.row.out_product_id, scope.$index)"
              >删除</el-button
            >
            <el-button
              v-if="scope.row.status == 5"
              type="text"
              size="small"
              class="mr10"
              @click="delisting(scope.row.out_product_id)"
              >下架</el-button
            >
            <el-button
              v-if="scope.row.status != 5"
              type="text"
              size="small"
              @click="listing(scope.row.out_product_id)"
              >上架</el-button
            >
            <el-button
              v-if="scope.row.edit_status == 3"
              type="text"
              size="small"
              @click="open(scope.row.reject_reason)"
              >查看失败原因</el-button
            >
            
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableForm.limit"
          :current-page="tableForm.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--详情-->
    <!--添加商品-->
    <el-dialog
      title="添加"
      :visible.sync="dialogVisible"
      width="700px"
      :before-close="handleClose"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="200px"
        class="demo-ruleForm"
        width= "60%"
      >
        <el-form-item label="选择类目" prop="third_cat_id">
          <el-select
            v-model="ruleForm.third_cat_id"
            filterable
            placeholder="请选择"
            class="selWidth"
          >
            <el-option
              v-for="item in categoryList"
              :key="item.third_cat_id"
              :label="item.cat_name"
              :value="item.third_cat_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="选择商品" prop="image">
          <div class="upLoadPicBox" @click="selectProduct">
            <div v-if="ruleForm.image" class="pictrue">
                <img :src="ruleForm.image" />
            </div>
            <div v-else class="upLoad">
                <i class="el-icon-camera cameraIconfont" />
            </div>
          </div>
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            :loading="Loading"
            @click="submitForm('ruleForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
    <goods-list ref="goodsList" @getProduct="getProduct" />
    <!--修改类目-->
    <el-dialog
      title="修改类目"
      :visible.sync="cateDialogVisible"
      width="700px"
      :before-close="handleClose2"
    >
      <el-form
        ref="cateForm"
        :model="cateForm"
        :rules="rules"
        label-width="200px"
        class="demo-ruleForm"
        width= "60%"
      >
        <el-form-item label="选择类目" prop="third_cat_id">
          <el-select
            v-model="cateForm.third_cat_id"
            filterable
            placeholder="请选择"
            class="selWidth"
          >
            <el-option
              v-for="item in categoryList"
              :key="item.third_cat_id"
              :label="item.cat_name"
              :value="item.third_cat_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="Loading"
            @click="submitForm('cateForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import goodsList from "./goodsList";
import {
    productListApi,
    produCreateApi,
    productUpdateApi,
    listingApi,
    delistingApi,
    chanegCateApi,
    categoryListApi,
    deleteApi,
    syncProductApi
} from "@/api/ministore";

import { roterPre } from "@/settings";
import { destoryApi, productDeleteApi } from "@/api/product";
export default {
  name: "ministoreProList",
  components: {goodsList},
  data() {
    return {
      Loading: false,
      roterPre: roterPre,
      listLoading: true,
      broadcast_goods_id: "",
      dialogVisible: false,
      cateDialogVisible: false,
      tableData: {
        data: [],
        total: 0,
      },
      tableForm: {
        page: 1,
        limit: 20,
        status_tag: this.$route.query.status ? this.$route.query.status : "",
        keyword: "",
        broadcast_goods_id: this.$route.query.id ? this.$route.query.id : "",
      },
      broadcast_goods_id: this.$route.query.id ? this.$route.query.id : "",
      liveRoomStatus: "",
      ruleForm: {
        image: '',
        product_id: '',
        third_cat_id: ''
      },
      rules: {
        third_cat_id: [
          { required: true, message: "请选择类目", trigger: "change" },
        ],
        product_id: [
          { required: true, message: "请选择商品", trigger: "change" },
        ],
        image: [
          { required: true, message: "请选择商品", trigger: "change" },
        ],
      },
      categoryList: [],
      cateForm: {
        third_cat_id: '',
        product_id: ''
      }
    };
  },
  watch: {
    // broadcast_goods_id(newName, oldName) {
    //   this.getList("");
    // },
  },
  mounted() {
    this.getList();
    this.getCategory();
  },
  methods: {
    // 批量添加直播商品
    batchAdd() {
      this.$refs.batchAdd.dialogVisible = true;
      this.$refs.batchAdd.getList([]);
    },
    // 详情
    onProDetails(id) {

    },
    // 编辑
    handleUpdate(id) {
      // this.$modalForm(updateBroadcastApi(id)).then(() => this.getList());
    },
    // 删除
    handleDelete(id, idx) {
      this.$modalSureDelete().then(() => {
        this.listLoading = true;
        deleteApi({ids: id})
          .then(({ message }) => {
            console.log(message);
            this.$message.success(message);
            this.getList();
            // this.tableData.data.splice(idx, 1);
          })
          .catch(({ message }) => {
            this.listLoading = false;
            this.$message.error(message);
          });
      });
    },
    handleSizeChangeIssue(val) {
      this.tableFormIssue.limit = val;
      this.getIssueList();
    },
    // 列表
    getList() {
      this.listLoading = true;
      console.log(this.tableForm);
      productListApi(this.tableForm)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableForm.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableForm.limit = val;
      this.getList();
    },
    // 修改状态
    onchangeIsShow(row) {
      changeProDisplayApi(row.broadcast_goods_id, { is_show: row.is_mer_show })
        .then(({ message }) => {
          this.$message.success(message);
          this.getList();
        })
        .catch(({ message }) => {
          this.$message.error(message);
        });
    },
    // 上架
    listing(id) {
      this.listLoading = true;
      listingApi({product_ids: id}).then((res) => {
        this.getList();
        this.$message.success(res.message);
      })
      .catch((res) => {
        this.listLoading = false;
        this.$message.error(res.message);
      });
    },
    //下架
    delisting(id) {
      this.listLoading = true;
      delistingApi({product_ids: id}).then((res) => {
        this.getList();
        this.$message.success(res.message);
      })
      .catch((res) => {
        this.listLoading = false;
        this.$message.error(res.message);
      });
    },
    open(text) {
      this.$alert(text, '失败原因', {
        confirmButtonText: '确定',
      });
    },
    handleClose(){
      this.dialogVisible = false;
      this.$refs["ruleForm"].resetFields();
    },
    handleClose2(){
      this.cateDialogVisible = false;
      this.$refs["cateForm"].resetFields();
      this.Loading = false;
    },
    // 提交添加商品表单
    submitForm(type) {
      let that = this
      this.$refs[type].validate((valid) => {
        if (!valid) {
          console.log('error submit!!');
          return false;
        } else {
          this.Loading = true;
          if (type == 'ruleForm') {
            console.log(this.ruleForm);
            produCreateApi(this.ruleForm).then((res) => {
              this.Loading = false;
              this.dialogVisible = false;
              this.getList();
              this.$message.success(res.message);
            })
            .catch((res) => {
              this.Loading = false;
              this.$message.error(res.message);
            });
          } else if (type == 'cateForm') {
            chanegCateApi(this.cateForm).then((res) => {
              this.cateDialogVisible = false;
              this.Loading = false;
              this.getList();
              this.$message.success(res.message);
            }).catch((res) => {
              this.Loading = false;
              this.$message.error(res.message);
            })
          }
        }
      });
          
        
      
    },
    goAdd() {
      this.dialogVisible = true;
    },
    // 同步商品
    sync() {
      let loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      syncProductApi({}).then((res) => {
        loading.close();
        this.getList();
        this.$message.success(res.message);
      })
      .catch((res) => {
        loading.close();
        this.$message.error(res.message);
      });
    },
    selectProduct()
    {
      this.$refs.goodsList.dialogVisible = true;
    },
    getProduct(data) {
        this.ruleForm.image = data.src;
        this.ruleForm.product_id = data.id;
    },
    getCategory() {
      categoryListApi({limit: 9999999})
        .then((res) => {
          this.categoryList = res.data.list;
          // this.tableData.total = res.data.count;
          // this.listLoading = false;
        })
        .catch((res) => {
          // this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    tochangeCate(product_id) {
      this.cateDialogVisible = true;
      this.cateForm.product_id = product_id;
    }
  },
};
</script>

<style scoped lang="scss">
.modalbox {
  /deep/.el-dialog {
    min-width: 550px;
  }
}
.selWidth {
  width: 400px;
}
.seachTiele {
  line-height: 35px;
}
.fa {
  color: #0a6aa1;
  display: block;
}
.sheng {
  color: #ff0000;
  display: block;
}
</style>
